package yingyong;

import gongju.DBUtil;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet(name = "Servletupdate", value = "/Servletupdate")
public class Servletupdate extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String name = request.getParameter("name");
        String sex = request.getParameter("sex");
        String id = request.getParameter("id");
        String sql = "update student set name = ?,sex = ? where id = ?";
        int i = 0;
        try {
            i = DBUtil.update(sql, name, sex, id);
        if (i>0){
            response.getWriter().write("<script>alert('修改成功');location.href='/index.jsp';</script>");
        }else {
            response.getWriter().write("修改失败");
        }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
